/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.portlet.editor.gui.view.internal.panel;

import com.genia.toolbox.basics.editor.gui.view.internal.panel.AbstractViewPanel;
import com.genia.toolbox.basics.editor.gui.view.internal.panel.ElementViewPanel;
import com.genia.toolbox.portlet.editor.gui.PortletEditorGUI;
import com.genia.toolbox.portlet.editor.gui.view.internal.PortletDocumentInternalFrame;
import com.genia.toolbox.portlet.editor.model.bean.PortletInitialSettings;
import com.genia.toolbox.portlet.editor.model.portlet.PortletType;
import com.genia.toolbox.portlet.editor.model.portlet.impl.ContainerPortletModel;
import com.genia.toolbox.portlet.editor.model.portlet.impl.PortletModel;

/**
 * The abstract view panel.
 */
@SuppressWarnings("serial")
public abstract class AbstractPortletViewPanel
    extends AbstractViewPanel<PortletEditorGUI, PortletDocumentInternalFrame, PortletModel, PortletType>
{

  /**
   * Constructor of a root panel.
   * 
   * @param portletEditorGUI
   *          the portletEditorGUI.
   * @param internalFrame
   *          the internal frame.
   */
  public AbstractPortletViewPanel(PortletEditorGUI portletEditorGUI, PortletDocumentInternalFrame internalFrame)
  {
    super(portletEditorGUI, internalFrame);
  }



  /**
   * Constructor of a sub-panel.
   * 
   * @param internalFrame
   *          the internal frame.
   * @param parentPanel
   *          the parent panel.
   */
  public AbstractPortletViewPanel(PortletDocumentInternalFrame internalFrame, ContainerPortletViewPanel parentPanel)
  {
    super(internalFrame, parentPanel);
  }



  /**
   * Add a sub-portlet.
   */
  @Override
  public void addSubElement()
  {
    ContainerPortletModel containerPortletModel = (ContainerPortletModel) this.getElement();
    PortletInitialSettings settings = this.getGui().createSubElement();
    this.getGui().getController().notifyCreateSubElement(settings, containerPortletModel.getDocumentID(), containerPortletModel.getUniqueID(), containerPortletModel.getDirection());
  }



  /**
   * Delete a sub-portlet.
   */
  @Override
  public void deleteSubElement()
  {
    this.getInternalFrame().notifyDocumentChanges();
    this.getGui().displayElementSettings(null);
    ContainerPortletViewPanel containerPanel = (ContainerPortletViewPanel) this.getParentPanel();
    containerPanel.deactivateAll();
    containerPanel.removeSubPortlet(this.getElement());
    this.updateBorder();
    ElementViewPanel<PortletEditorGUI, PortletDocumentInternalFrame, PortletModel, PortletType> panel = this;
    while (panel != null) {
      panel.updateBorder();
      panel = panel.getParentPanel();
    }
  }



  /**
   * Move down or left a sub-portlet.
   */
  @Override
  public void moveDownOrLeft()
  {
    this.getInternalFrame().notifyDocumentChanges();
    ContainerPortletViewPanel containerPanel = (ContainerPortletViewPanel) this.getParentPanel();
    containerPanel.moveDownSubPortlet(this.getElement());
  }



  /**
   * Move up or right a sub-portlet.
   */
  @Override
  public void moveUpOrRight()
  {
    this.getInternalFrame().notifyDocumentChanges();
    ContainerPortletViewPanel containerPanel = (ContainerPortletViewPanel) this.getParentPanel();
    containerPanel.moveUpSubPortlet(this.getElement());
  }



  /**
   * Get the id label text.
   * 
   * @param model
   *          the model.
   * @return the id label text.
   */
  @Override
  public String getLabelIdText(PortletModel model)
  {
    String text = "";
    if (model != null && model.getId() != null) {
      text = model.getId();
    }
    return text;
  }

}